package top.mcwebsite.playandroid.business.user_article.impl.ui

import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.IntrinsicSize
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.material3.Text
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Favorite
import androidx.compose.material.icons.filled.FavoriteBorder
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.Divider
import androidx.compose.material3.Icon
import androidx.compose.material3.LocalContentColor
import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.NavController
import androidx.paging.LoadState
import androidx.paging.compose.LazyPagingItems
import androidx.paging.compose.collectAsLazyPagingItems
import top.mcwebsite.lib.route.api.EasyRouter
import top.mcwebsite.playandroid.business.articles.api.model.ArticleModel
import top.mcwebsite.playandroid.business.user_article.impl.vm.UserArticleIntent
import top.mcwebsite.playandroid.business.user_article.impl.vm.UserArticleUIState
import top.mcwebsite.playandroid.business.user_article.impl.vm.UserArticleUiEffect
import top.mcwebsite.playandroid.business.user_article.impl.vm.UserArticleViewModel
import top.mcwebsite.playandroid.common.constant.RouteConfig
import top.mcwebsite.playandroid.common.list.EasyLazyListPageColumn
import top.mcwebsite.playandroid.common.network.ApiException
import top.mcwebsite.playandroid.common.network.NetworkApiException
import top.mcwebsite.playandroid.common.uiWidget.ErrorModel
import top.mcwebsite.playandroid.common.uiWidget.ErrorPage
import top.mcwebsite.playandroid.common.uiWidget.SkeletonPage
import top.mcwebsite.playandroid.lib.common.ext.encodeURL
import top.mcwebsite.playandroid.strings.Str

@Composable
fun UserArticlePage() {
    val viewModel: UserArticleViewModel = viewModel()
    viewModel.HandleUiEffect {
        when (it) {
            is UserArticleUiEffect.GoDetailPageUiEffect -> {
                EasyRouter.navigate("${RouteConfig.ArticleDetail.PATH}/${it.articleModel.link.encodeURL()}")
            }

            is UserArticleUiEffect.GoLoginPageUiEffect -> {
                EasyRouter.navigate(RouteConfig.Login.PATH)
            }
        }
    }
    val lazyPagingItems = viewModel.pageFlow.collectAsLazyPagingItems()
    val uiState by viewModel.uiStateFlow.collectAsState()

//    val pullRefreshState = rememberPullRefreshState(refreshing = uiState.refreshing, onRefresh = {
//        if (lazyPagingItems.loadState.refresh is LoadState.NotLoading) {
//            viewModel.refresh {
//                lazyPagingItems.refresh()
//            }
//        }
//    })
    EasyLazyListPageColumn(
        lazyPagingItems = lazyPagingItems,
        loading = {
            SkeletonPage {
                SkeletonItem()
            }
        },
        loadingMoreError = {

        }) { index, item ->
        UserArticleItem(
            articleModel = item,
            goDetailPage = { article ->
                viewModel.sendIntent(UserArticleIntent.GoDetailPageIntent(article))
            },
        )
    }
}


@Composable
fun UserArticleItem(
    articleModel: ArticleModel,
    goDetailPage: (ArticleModel) -> Unit,
) {
    Column(
        modifier = Modifier
            .clickable {
                goDetailPage(articleModel)
            }
            .padding(vertical = 5.dp, horizontal = 12.dp)
    ) {
        Text(
            text = articleModel.title,
            style = MaterialTheme.typography.titleMedium,
            modifier = Modifier.fillMaxSize()
        )
        Row(
            modifier = Modifier.padding(top = 3.dp)
        ) {
            Text(
                text = stringResource(id = Str.shared_by, articleModel.shareUser),
                style = MaterialTheme.typography.labelMedium,
                color = LocalContentColor.current.copy(0.7f)
            )
            Text(
                text = stringResource(id = Str.time_is, articleModel.niceDate),
                style = MaterialTheme.typography.labelMedium,
                color = LocalContentColor.current.copy(0.7f),
                modifier = Modifier.padding(start = 5.dp),
            )
        }
    }
}